package alp.starcode.user.service;

import alp.starcode.framework.db.entity.*;
import alp.starcode.framework.db.mapper.*;
import alp.starcode.framework.enums.UserTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.UUID;

@Service
public class UserService {

    @Autowired
    UsersMapper usersMapper;

    @Autowired
    StoreInfoMapper storeInfoMapper;

    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Autowired
    OrderDetailInfoMapper orderDetailInfoMapper;

    @Autowired
    CommentsMapper commentsMapper;

    /**
     * 新增买家，外卖员，管理员
     * @param userAccount
     * @param userPassword
     * @param userName
     * @param type
     * @return
     */
    public Boolean registerBuyers(String userAccount,String userPassword,String userName,String type){
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getUserAccount,userAccount);
        Users users = usersMapper.selectOne(queryWrapper);
        if (users!=null){
            return false;
        }
        Users build = Users.builder()
                .userAccount(userAccount)
                .userName(userName)
                .userPassword(userPassword)
                .userUuid(UUID.randomUUID().toString())
                .userType(type)
                .build();
        return usersMapper.insert(build)>0;
    }

    /**
     * 新增商家
     * @param userAccount
     * @param userPassword
     * @param userName
     * @param storeName
     * @param storeAddress
     * @param storePhone
     * @return
     */
    public Boolean registerSeller(String userAccount,String userPassword,String userName,String storeName,String storeAddress,String storePhone){
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getUserAccount,userAccount);
        Users users = usersMapper.selectOne(queryWrapper);
        if (users!=null){
            return false;
        }
        Users build = Users.builder()
                .userAccount(userAccount)
                .userName(userName)
                .userPassword(userPassword)
                .userUuid(UUID.randomUUID().toString())
                .userType(UserTypeEnum.USER_TYPE_SELL.getType())
                .build();
        if (usersMapper.insert(build)>0){
            StoreInfo build1 = StoreInfo.builder()
                    .storeUuid(UUID.randomUUID().toString())
                    .storeAddress(storeAddress)
                    .storeName(storeName)
                    .storePhone(storePhone)
                    .build();
            return storeInfoMapper.insert(build1)>0;
        }else {
            return false;
        }

    }

    /**
     *
     * @param userAccount
     * @param userPassword
     * @return
     */
    public Users login(String userAccount, String userPassword, HttpSession httpSession){
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getUserAccount,userAccount);
        queryWrapper.eq(Users::getUserPassword,userPassword);
        Users users = usersMapper.selectOne(queryWrapper);
        httpSession.setAttribute("users",users);
        return users;
    }

    /**
     * 下单
     * @param userUuid
     * @param storeUuid
     * @param remark
     * @param address
     * @param totalPrice
     * @param merchandiseUuid
     * @param merchandiseNum
     * @param price
     * @return
     */
    public Boolean buy(String userUuid,String storeUuid,String remark,String address,String totalPrice,String merchandiseUuid,String merchandiseNum,String price){
        String orderUuid = UUID.randomUUID().toString() ;
        OrderInfo build = OrderInfo.builder()
                .userUuid(userUuid)
                .address(address)
                .storeUuid(storeUuid)
                .totalPrice(new BigDecimal(totalPrice))
                .remark(remark)
                .createTime(System.currentTimeMillis())
                .orderUuid(orderUuid)
                .build();
        if(orderInfoMapper.insert(build)>0){
            OrderDetailInfo build1 = OrderDetailInfo.builder()
                    .merchandiseNum(Integer.valueOf(merchandiseNum))
                    .merchandiseUuid(merchandiseUuid)
                    .price(new BigDecimal(price))
                    .orderUuid(orderUuid)
                    .build();
            return orderDetailInfoMapper.insert(build1)>0;
        }
        return false;

    }

    public Boolean insertComment(String userUuid,String orderUuid,String msg){
        Comments comments = Comments.builder()
                .commentsContent(msg)
                .userUuid(userUuid)
                .createTime(System.currentTimeMillis())
                .orderUuid(orderUuid)
                .commentsUuid(UUID.randomUUID().toString())
                .build();
        return commentsMapper.insert(comments)>0;
    }

}
